//
//  QcNewFeatureViewController.m
//  QcMedical1
//
//  Created by ios001 on 2016/12/23.
//  Copyright © 2016年 jiawen Du. All rights reserved.
//

#import "QcNewFeatureViewController.h"
#import "QcFeatureCell.h"
#import "AppDelegate.h"
#import "JASidePanelController.h"
#import "LeftViewController.h"
#import "HomeViewController.h"

@interface QcNewFeatureViewController ()

@property (nonatomic ,weak) UIButton *skipBtn;

@end

@implementation QcNewFeatureViewController

static NSString * const reuseIdentifier = @"FeatureCell";


- (instancetype)init{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.itemSize = [UIScreen mainScreen].bounds.size;
    
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    
    return [super initWithCollectionViewLayout:layout];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    self.collectionView.bounces = NO;
    self.collectionView.pagingEnabled = YES;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    
    [self.collectionView registerClass:[QcFeatureCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
    UIPageControl *page = [[UIPageControl alloc]init];
    
    page.numberOfPages = 4;
    
    page.currentPage = 0;
    
    page.currentPageIndicatorTintColor = [UIColor redColor];
    
    [self.collectionView addSubview:page];
    
    [page mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerX.equalTo(self.collectionView);
        make.bottom.equalTo(self.collectionView).mas_offset(-30);
        
    }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 4;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    QcFeatureCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FeatureCell" forIndexPath:indexPath];
    NSString *imageName = [NSString stringWithFormat:@"welcome_no%ld",indexPath.item+1];
    cell.imageName = imageName;
    cell.contentView.backgroundColor = [UIColor whiteColor];
    return cell;
}


#pragma mark - 停止减速
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    //偏移量
    CGFloat offsetX = scrollView.contentOffset.x;
    
    int index = offsetX / SCREEN_WIDTH;
    
    if (index == 3) {
        
        self.skipBtn.hidden = NO;
    }else{
        
        self.skipBtn.hidden = YES;
    }
}


#pragma mark - 跳转到home控制器
- (void)skip{
    
    AppDelegate *appd = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setBool:YES forKey:@"kisFirstEnter"];
    
    
    //实例化
    appd.viewController = [[JASidePanelController alloc] init];
    
    // 左
    appd.viewController.leftPanel = [[LeftViewController alloc] init];
    
    // 右
    HomeViewController *homeVc = [[HomeViewController alloc] init];
    appd.viewController.centerPanel = [[UINavigationController alloc] initWithRootViewController:homeVc];
    
    //显示window
    appd.window.rootViewController = appd.viewController;
    
    [appd.window makeKeyAndVisible];
    
    CATransition *sition = [CATransition animation];
    
    sition.type = @"rippleEffect";
    sition.duration = 1;
    [appd.window.layer addAnimation:sition forKey:nil];
}



- (UIButton *)skipBtn{
    if (!_skipBtn) {
        UIButton *skipBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        _skipBtn = skipBtn;
        
        [_skipBtn setBackgroundImage:[UIImage imageNamed:@"guideStart"] forState:UIControlStateNormal];
        [_skipBtn sizeToFit];
        _skipBtn.center = CGPointMake(SCREEN_WIDTH*3.5, SCREEN_HEIGHT*0.95);
        
        [_skipBtn addTarget:self action:@selector(skip) forControlEvents:UIControlEventTouchUpInside];
        [self.collectionView addSubview:_skipBtn];
    }
    return _skipBtn;
}



@end




